import axios from 'axios';
import * as apiUrl from './URL';
import { notification } from 'antd';

/**
 *  接口请求数据时执行的方法
 *  接受参数为请求的路径apiUrl、请求接口配置参数configObj
 *
 * @param {String} apiUrl            用户传入的请求路径
 * @param {Object} configObj        用户传入的接口参数
 */
export function getServer(apiUrl, data, responseType) {
  /**
   * 返回的Promise对象含有then、catch方法
   */
  return new Promise(function (resolve, reject) {
    axios({
      url: apiUrl,
      method: 'GET',
      params: data,
      headers: {
        'Content-Type': 'application/json', //,
        Authorization: `Bearer ${window.sessionStorage.getItem('tokens')} ` || '',
      },
      responseType,
    })
      .then(function (response) {
        if (response.status === 200 && response.data) {
          let res = response.data;
          switch (res.code) {
            case 0:
              resolve(res);
              break;
            case 400:
              notification.error({
                message: '请求数据格式不符！',
              });
              reject(res);
              break;
            case 403:
              notification.error({
                message: '未授权，请重新登录!',
              });
              reject(res);
              break;
            case 404:
              notification.error({
                message: '未找到该资源！',
              });
              reject(res);
              break;
            case 500:
              notification.error({
                message: '服务器内部错误！',
              });
              reject(res);
              break;
            case 503:
              notification.error({
                message: '服务器正忙，请稍后再试！',
              });
              reject(res);
              break;

            default:
              break;
          }
        } else {
          notification.error({
            message: '操作失败',
            description: '服务器错误',
          });
          resolve(response);
        }
      })
      .catch(function (error) {
        notification.error({
          message: '操作失败',
          description: '网络异常,请稍后重试',
        });
        reject(error);
      });
  });
}
export function postServer(apiUrl, data) {
  /**
   * 返回的Promise对象含有then、catch方法
   */
  return new Promise((resolve, reject) => {
    axios({
      url: apiUrl,
      method: 'POST',
      //params: params || {},
      data: data || {},
      headers: {
        accept: '*/*',
        'Content-Type': 'application/json',
        Authorization: `Bearer ${window.sessionStorage.getItem('tokens')} ` || '',
      },
    })
      .then((response) => {
        if (response.status === 200 && response.data) {
          let res = response.data;
          switch (res.code) {
            case 0:
              resolve(res);
              break;
            case 400:
              notification.error({
                message: '请求数据格式不符！',
              });
              reject(res);
              break;
            case 403:
              notification.error({
                message: '拒绝访问！',
              });
              reject(res);
              break;
            case 404:
              notification.error({
                message: '未找到该资源！',
              });
              reject(res);
              break;
            case 500:
              notification.error({
                message: '服务器内部错误！',
              });
              reject(res);
              break;
            case 503:
              notification.error({
                message: '服务器正忙，请稍后再试！',
              });
              reject(res);
              break;

            default:
                notification.error({
                    message: '请求异常！',
                    });
                    reject(res);
                break;
          }
        } else {
          notification.error({
            message: '操作失败',
            description: '服务器错误',
          });
          resolve(response);
        }
      })
      .catch((error) => {
        notification.error({
          message: '操作失败',
          description: '网络异常,请稍后重试',
        });
        reject(error);
      });
  });
}

// put getPutDataFromServer
export function putServer(apiUrl, data) {
  return new Promise((resolve, reject) => {
    axios({
      url: apiUrl,
      method: 'PUT',
      //params: params || {},
      data: data || {},
      headers: {
        'Content-Type': 'application/json', //,
        Authorization: `Bearer ${window.sessionStorage.getItem('tokens')} ` || '',
      },
    })
      .then((response) => {
        if (response.status === 200 && response.data) {
          let res = response.data;
          switch (res.code) {
            case 0:
              resolve(res);
              break;
            case -1:
              notification.error({
                message: '请求错误！',
              });
              reject(res);
              break;
            case 400:
              notification.error({
                message: '请求数据格式不符！',
              });
              reject(res);
              break;
            case 403:
              notification.error({
                message: '拒绝访问！',
              });
              reject(res);
              break;
            case 404:
              notification.error({
                message: '未找到该资源！',
              });
              reject(res);
              break;
            case 500:
              notification.error({
                message: '服务器内部错误！',
              });
              reject(res);
              break;
            case 503:
              notification.error({
                message: '服务器正忙，请稍后再试！',
              });
              reject(res);
              break;

            default:
              break;
          }
        } else {
          notification.error({
            message: '操作失败',
            description: '服务器错误',
          });
          resolve(response);
        }
      })
      .catch((error) => {
        notification.error({
          message: '操作失败',
          description: '网络异常,请稍后重试',
        });
        reject(error);
      });
  });
}
export function deleteServer(apiUrl, data, responseType) {
  /**
   * 返回的Promise对象含有then、catch方法
   */
  return new Promise(function (resolve, reject) {
    axios({
      url: apiUrl,
      method: 'DELETE',
      params: data,
      headers: {
        'Content-Type': 'application/json', //,
        Authorization: `Bearer ${window.sessionStorage.getItem('token')} ` || '',
      },
      responseType,
    })
      .then(function (response) {
        if (response.status === 200 && response.data) {
          let res = response.data;
          switch (res.code) {
            case 0:
              resolve(res);
              break;
            case 1:
              resolve(res);
              break;
            case -1:
              notification.error({
                message: '请求错误！',
              });
              reject(res);
              break;
            case 400:
              notification.error({
                message: '请求数据格式不符！',
              });
              reject(res);
              break;
            case 403:
              notification.error({
                message: '拒绝访问！',
              });
              reject(res);
              break;
            case 404:
              notification.error({
                message: '未找到该资源！',
              });
              reject(res);
              break;
            case 500:
              notification.error({
                message: '服务器内部错误！',
              });
              reject(res);
              break;
            case 503:
              notification.error({
                message: '服务器正忙，请稍后再试！',
              });
              reject(res);
              break;

            default:
              break;
          }
        } else {
          notification.error({
            message: '操作失败',
            description: '服务器错误', 
          });
          resolve(response);
        }
      })
      .catch(function (error) {
        notification.error({
          message: '操作失败',
          description: '网络异常,请稍后重试',
        });
        reject(error);
      });
  });
}

// 登录请求
export function login(params) {
    return postServer(apiUrl.loginUrl, params);
}
export function infoList() {
  return getServer(apiUrl.infoList);
}
